var i = 1;

function recognition(){
	context.drawImage(video, 100, 40, 440, 440, 0, 0, 320, 240);
  	//image = context.getImageData(0, 0, 320, 240);
  	var dataURL = canvas.toDataURL('images/png');
    var imageDataB64 = dataURL.substring(22);
    var Sctid = cid;
    var Sid = 0;
    var stateT;
	var statecolor;
	var flag;
	var myDate = new Date();
	
	$.ajax(
		{
			type:"post",
			url:"recognition.action",
			dataType:"json",
			data:{
				testImage: imageDataB64,
				Stcid: Sctid
			},
			success:function(result){
				flag = result.state;
				Sid = result.sid;
				if(flag == 0){
					alert(Sid+"同学，现在不在上课时间段内");
				} else if(flag == 3){
					setTrackerState(4);
				} else if (flag == 4){
					setTrackerState(3);
				} else {
					if(flag == 1) {
			    		stateT = "已签到";
			    		statecolor = "btn-success";
			    	} else {
			    		stateT = "已迟到";
			    		statecolor = "btn-warning";
			    	}
					setTrackerState(2);
			    	$("#detection-log ul").prepend($("<li></li>").text(Sid + stateT + "————" + myDate.toLocaleString() + " NO:" + i));
			    	$("#" + Sid + "C > button").attr('class','btn '+statecolor+' btn-xs').text(stateT);
					$("#" + Sid + "D").text(myDate.toLocaleString());
					$("#" + Sid + "T").attr('class','state'+flag);
					i++;
				}
		    }
		});
  		
  		if(i>11){
  			$("#detection-log ul li").eq(-1).remove();
  		}
}

function success(sid,sctid,Flag) {
	var state;
	var statecolor;
	var flag = Flag;
	var myDate = new Date();
	
	if($("#" + sid + "T").attr('class')=="state3"){
		if(flag == 1) {
    		state = "已签到";
    		statecolor = "btn-success";
    	} else {
    		state = "已迟到";
    		statecolor = "btn-warning";
    	}
    	$("#detection-log ul").prepend($("<li></li>").text(sid + state + "————" + myDate.toLocaleString() + " NO:" + i));
    	$("#" + sid + "C > button").attr('class','btn '+statecolor+' btn-xs').text(state);
		$("#" + sid + "D").text(myDate.toLocaleString());
		$("#" + sid + "T").attr('class','state'+flag);
		i++;
	} else {
		alert("已经记录过了");
	}
	
	if(i>11){
		$("#detection-log ul li").eq(-1).remove();
	}
}

function setTrackerState(trackerState) {
	var string;
	var color;
	switch (trackerState) {
	case 0:
		string = "待机中";
		color = "#888888";
		break;
	case 1:
		string = "提交数据中";
		color = "#FFD934";
		break;
	case 2:
		string = "已记录";
		color = "#42B274";
		var t=setTimeout("setTrackerState(0)",2000);
		break;
	case 3:
		string = "失败，请重试！";
		color = "#FF5E5E";
		var t=setTimeout("setTrackerState(0)",2000);
		break;
	case 4:
		string = "你已签到过了！";
		color = "#FF5E5E";
		var t=setTimeout("setTrackerState(0)",2000);
		break;
	case 5:
		string = "距离太远，请靠近";
		color = "#888888";
		break;
	default:
		break;
	}
	$("#trackerState").text(string).css("color", color);
}



$(function() {
	
});